//
//  NSString+Extension.m
//  JiaoX
//
//  Created by yinqy on 15/2/5.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)
-(CGSize)sizeWithFont:(UIFont *)font{
    return [self sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
}
-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    return [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil].size;
}

-(NSString *)trim{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

+(NSString *)randomStringWithLength:(NSUInteger)length{
    NSUInteger NUMBER_OF_CHARS = length;
    char data[NUMBER_OF_CHARS];
    for (int x=0;x<NUMBER_OF_CHARS;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
}

- (NSString *)replaceCharacter:(NSString *)oStr withString:(NSString *)nStr
{
    NSMutableString *_str = [NSMutableString stringWithString:self];
    [_str replaceOccurrencesOfString:oStr withString:nStr options:NSCaseInsensitiveSearch range:NSMakeRange(0, _str.length)];
    return _str;
}

+ (NSString *)pathDocumentsWithFileName:(NSString *)filename{
    NSString *pngDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *FileFullPath = [pngDir stringByAppendingPathComponent:filename];
    return FileFullPath;
}

+(BOOL)isBlankString:(NSString *)string{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if(![string isKindOfClass:[NSString class]]){
        string = [NSString stringWithFormat:@"%@",string];
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
@end
